package com.nervenets.shopping.hibernate.domain.user;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.annotation.JSONField;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.nervenets.general.hibernate.TreeEntityDomainObject;
import com.nervenets.general.utils.SpringContextHolder;
import com.nervenets.shopping.enumeration.Gender;
import com.nervenets.shopping.hibernate.domain.user.vo.UserVo;
import com.nervenets.shopping.service.UserService;
import io.swagger.annotations.ApiModelProperty;
import lombok.*;
import lombok.experimental.SuperBuilder;

import javax.persistence.*;
import java.util.ArrayList;
import java.util.List;

/**
 * 用户
 */
@EqualsAndHashCode(callSuper = true)
@Entity
@Table
@Setter
@Getter
@AllArgsConstructor
@NoArgsConstructor
@SuperBuilder
public class User extends TreeEntityDomainObject<User> {
    @ApiModelProperty(value = "账号")
    @org.hibernate.annotations.Index(name = "_idx_user_mobile")
    @Column(columnDefinition = "varchar(20)")
    private String mobile;
    @ApiModelProperty(value = "密码")
    @Column(columnDefinition = "varchar(20)")
    @JsonIgnore
    @JSONField(serialize = false)
    private String password;
    @ApiModelProperty(value = "头像")
    private String avatar;
    @ApiModelProperty(value = "微信unionId")
    @Column(columnDefinition = "varchar(64)")
    private String unionId;
    @ApiModelProperty(value = "性别")
    @Column(columnDefinition = "tinyint")
    @Builder.Default
    private Gender gender = Gender.female;
    @ApiModelProperty(value = "是否是团长")
    @Builder.Default
    private boolean inviter = false;
    @ApiModelProperty(value = "是否是优秀团长")
    @Builder.Default
    private boolean excellentInviter = false;
    @ApiModelProperty(value = "生日")
    private long birthday;
    @ApiModelProperty(value = "最后登录时间")
    private long lastLoginTime;
    @ApiModelProperty(value = "最后拼团时间")
    private long lastGroupTime;
    @ApiModelProperty(value = "拼团次数")
    private long groupCount;
    @Column(columnDefinition = "varchar(20)")
    @ApiModelProperty(value = "省份")
    private String province;
    @Column(columnDefinition = "varchar(20)")
    @ApiModelProperty(value = "城市")
    private String city;
    @ApiModelProperty(value = "经度")
    private double longitude = 0;
    @ApiModelProperty(value = "纬度")
    private double latitude = 0;
    @ApiModelProperty(value = "邀请数量")
    private long inviteCount;
    @ApiModelProperty(value = "是否关注")
    private boolean subscribe;
    @ApiModelProperty(value = "附加内容")
    @OneToMany(cascade = CascadeType.ALL, orphanRemoval = true)
    @OrderBy(value = "createTime")
    @JoinColumn(name = "user_id")
    private List<UserExtra> extras = new ArrayList<>();
    @OneToMany(cascade = CascadeType.ALL, orphanRemoval = true)
    @OrderBy(value = "createTime")
    @JoinColumn(name = "user_id")
    private List<UserWxId> wxIds = new ArrayList<>();
    @Transient
    private JSONObject surplus;

    public User(String mobile, String password, String name, String avatar, String unionId, Gender gender, boolean enable) {
        this.mobile = mobile;
        this.password = password;
        setName(name);
        this.avatar = avatar;
        this.unionId = unionId;
        this.gender = gender;
        setEnable(enable);
    }

    public UserVo getInviteUser() {
        if (getParentId() > 0) {
            UserService userService = SpringContextHolder.getBean(UserService.class);
            User user = userService.fastFind(getParentId());
            if (null != user) return new UserVo(user);
        }
        return null;
    }
}
